﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ListPageContral : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
        else
        {
            if (JumpList.SelectedValue == "1")
            {
                lblCurrPage.Text = "1";
            }
        }
    }

    void init()
    {
        lblCurrPage.Text = "1";
        ButtonState();

    }

    void bindDllPage()
    {

    }

    public int CurrentPageIndex
    {
        get
        {
            return Convert.ToInt32(lblCurrPage.Text) - 1;
        }
    }

    //按钮的状态
    private void ButtonState()
    {
        btnEnd.Enabled = true;
        btnHome.Enabled = true;
        btnNext.Enabled = true;
        btnPrv.Enabled = true;
        //页数为1按钮都为不可用
        if (lblPageCount.Text == "1")
        {
            btnEnd.Enabled = false;
            btnHome.Enabled = false;
            btnNext.Enabled = false;
            btnPrv.Enabled = false;
        }
        else
        {
            //第一页按钮上一页和首页为不可用
            if (lblCurrPage.Text == "1")
            {
                btnHome.Enabled = false;
                btnPrv.Enabled = false;
            }
            else
                //最后一页时按钮下一页和尾页不可用
                if (lblCurrPage.Text == lblPageCount.Text)
                {
                    btnEnd.Enabled = false;
                    btnNext.Enabled = false;
                }
        }
    }


    /// <summary>
    /// 设置页数
    /// </summary>
    void SetPageCount()
    {
        int rowsCount = Convert.ToInt32(lblSum.Text);
        int pageSize = Convert.ToInt32(lblPageSize.Text);
        int pageCount = 1;
        //页数
        if (rowsCount != 0)
        {
            pageCount = rowsCount / pageSize;
            if (rowsCount % pageSize > 0)
            {
                pageCount++;
            }

            if (pageSize > rowsCount)
            {
                pageCount = 1;
            }


        }
        //梆定下拉列表
        lblPageCount.Text = pageCount.ToString();
        JumpList.Items.Clear();
        for (int i = 1; i <= pageCount; i++)
        {
            JumpList.Items.Add(i.ToString());
        }
    }
    /// <summary>
    /// 每页记录数
    /// </summary>
    public int PageSize
    {
        set
        {
            if (value <= 0)
            {
                lblPageSize.Text = "1";
            }
            else
            {
                lblPageSize.Text = value.ToString();
            }
            SetPageCount();
            ButtonState();
        }

        get
        {
            return Convert.ToInt32(lblPageSize.Text);
        }
    }

    /// <summary>
    /// 总记录数
    /// </summary>
    public int ItemCount
    {
        set
        {
            if (value < 0)
            {
                lblSum.Text = "0";
            }
            else
            {
                lblSum.Text = value.ToString();
            }
            SetPageCount();
            ButtonState();
        }
    }

    /// <summary>
    /// 页码改变触发
    /// </summary>
    /// <param name="pageSize">每一页的记录数</param>
    /// <param name="PageIndex">页码</param>

    public event PageSizeChangeEventHandler PageSizeChange;

    /// <summary>
    /// 上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPrv_Click(object sender, EventArgs e)
    {
        try
        {
            lblCurrPage.Text = Convert.ToString(Convert.ToInt32(lblCurrPage.Text) - 1);
            PageSizeChange(Convert.ToInt32(lblPageSize.Text), Convert.ToInt32(lblCurrPage.Text) - 1);
            ButtonState();
            JumpList.SelectedValue = lblCurrPage.Text;
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('上一页:" + "lblCurrPage.Text:" + lblCurrPage.Text + "JumpList.SelectedValue" + JumpList.SelectedValue + "/r/n" + ex.Message + "')</script>");
        }
    }
    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbHome_Click(object sender, EventArgs e)
    {
        try
        {
            lblCurrPage.Text = "1";
            PageSizeChange(Convert.ToInt32(lblPageSize.Text), Convert.ToInt32(lblCurrPage.Text) - 1);
            ButtonState();
            JumpList.SelectedValue = lblCurrPage.Text;
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('首页:" + "lblCurrPage.Text:" + lblCurrPage.Text + "JumpList.SelectedValue" + JumpList.SelectedValue + "/r/n" + ex.Message + "')</script>");
        }
    }
    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNext_Click(object sender, EventArgs e)
    {
        try
        {
            lblCurrPage.Text = Convert.ToString(Convert.ToInt32(lblCurrPage.Text) + 1);
            PageSizeChange(Convert.ToInt32(lblPageSize.Text), Convert.ToInt32(lblCurrPage.Text) - 1);
            ButtonState();
            JumpList.SelectedValue = lblCurrPage.Text;
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('下一页:" + "lblCurrPage.Text:" + lblCurrPage.Text + "JumpList.SelectedValue" + JumpList.SelectedValue+"/r/n" + ex.Message + "')</script>");
        }
    }
    /// <summary>
    /// 末页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEnd_Click(object sender, EventArgs e)
    {
        try
        {
            lblCurrPage.Text = Convert.ToString(lblPageCount.Text);
            PageSizeChange(Convert.ToInt32(lblPageSize.Text), Convert.ToInt32(lblCurrPage.Text) - 1);
            ButtonState();
            JumpList.SelectedValue = lblCurrPage.Text;
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('末页:" + "lblCurrPage.Text:" + lblCurrPage.Text + "JumpList.SelectedValue" + JumpList.SelectedValue + "/r/n" + ex.Message + "')</script>");
        }
    }
    /// <summary>
    /// 具体的页数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            lblCurrPage.Text = JumpList.SelectedItem.Text;
            PageSizeChange(Convert.ToInt32(lblPageSize.Text), Convert.ToInt32(lblCurrPage.Text) - 1);
            ButtonState();
            JumpList.SelectedValue = lblCurrPage.Text;
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('具体的页数:" + "lblCurrPage.Text:" + lblCurrPage.Text + "JumpList.SelectedValue" + JumpList.SelectedValue + "/r/n" + ex.Message + "')</script>");
        }
    }

    public void SetFirstPage()
    {
        lblCurrPage.Text = "1";
        ButtonState();
        JumpList.SelectedValue = lblCurrPage.Text;
    }

}
public delegate void PageSizeChangeEventHandler(int pageSize, int PageIndex);